﻿using Newtonsoft.Json;
using ShortcutMenu.Render;
using System;
using System.Collections.Generic;
using System.DrawingCore;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ShortcutMenu
{
    using DrawingImage = System.DrawingCore.Image;
    using DrawingPoint = System.DrawingCore.Point;

    public class ApplyFactory : ElementFactory, IElementFactory<ApplyElement, ApplyData>
    {
        public ApplyFactory() { }

        /// <summary>
        /// 当前二维位置数组的指针[该指针目前只使用在第一次加载]
        /// </summary>
        public static int CurrX = 0;
        public static int CurrY = 0;

        public ApplyElement Create(ApplyData data)
        {
            //存储当前指针指向的位置
            var apply = new ApplyElement(data);
            return apply;
        }
    }

    public class ApplyElement : FrameworkElement
    {
        /// <summary>
        /// 占位(1x1, 2x2, 3x3)
        /// </summary>
        public int Stations = 1;

        /// <summary>
        /// 占位矩阵
        /// </summary>
        public List<int> Matrix = new List<int>();

        /// <summary>
        /// 根元素
        /// </summary>
        public StackPanel RootElement;

        public Thickness Margin {
            get { return RootElement.Margin; }
            set { RootElement.Margin = value; }
        }

        #region 共同属性
        /// <summary>
        /// 边长
        /// </summary>
        public readonly static int _sideLength = 50;
        /// <summary>
        /// 外边距[Left,Top]
        /// </summary>
        public readonly static int _margin = 3;
        /// <summary>
        /// 背景色
        /// </summary>
        public readonly static SolidColorBrush _backColor = SY_Library.Color.BackColor(255, 255 / 2);
        /// <summary>
        /// 图像外边距[最小]
        /// </summary>
        public readonly static int _imageMargin = 7;
        #endregion

        public ApplyData Data;

        public ApplyElement(ApplyData data)
        {
            Stations = data.Size;
            Data = data;
            CreateNew();
        }

        private void CreateNew()
        {
            var length = (Stations * _sideLength) + ((Stations - 1) * _margin);
            RootElement = new StackPanel()
            {
                Width = length,
                Height = length,
                //Margin = new Thickness((point.X * (_sideLength + _margin)) + _margin, (point.Y * (_sideLength + _margin)) + _margin, 0, 0),
                Background = _backColor
            };

            System.Windows.Media.Brush brush = null;
            if (File.Exists(Data.IconPath))
            {
                if (Data.IsImageSave == 0)
                {
                    //进行保存,再进行读取
                    SaveImage();
                }
                brush = new ImageBrush(new BitmapImage(new Uri(Data.IconPath)));
            }

            var length01 = length - (_imageMargin * 2) - (Stations - 1) * 11;
            StackPanel imageSp = new StackPanel()
            {
                Background = brush == null ? SY_Library.Color.BackColor(255, 0, 0) : brush,
                Width = length01,
                Height = length01,
                Margin = new Thickness((length - length01) / 2)
            };

            RootElement.Children.Add(imageSp);
        }

        public void SetPoint()
        {

        }

        private void SaveImage()
        {
            DrawingImage image = DrawingImage.FromFile(Data.IconPath);
            image = SY_Library.ImageDispose.ToSquare(image);
            Data.IconPath = image.SaveImage(Data.Name, Path.GetExtension(Data.IconPath));
            Data.IsImageSave = 1;
        }


    }

    public static class DrawingImageExtend
    {
        public readonly static string _path = Directory.GetCurrentDirectory() + "/Image/ApplyIcon";
        public static string SaveImage(this DrawingImage image, string name, string suffix)
        {
            if (!Directory.Exists(_path))
                Directory.CreateDirectory(_path);
            var path = _path + "/" + name + suffix;
            if (!File.Exists(path))
            {
                image.Save(path);
                image.Clone();
            }
            return path;
        }
    }
}
